<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          node-key="id"
          :data="data"
          :props="defaultProps"
          default-expand-all
          highlight-current
          :expand-on-click-node="false"

          @node-click="handleNodeClick"
        />
      </div>
      <div class="right">
        <el-row class="operate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="daoru">excel导入</el-button>
          <el-button size="mini" @click="expro">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="rows">
          <el-table-column prop="staffPhoto" align="center" label="头像 ">
            <template v-slot="{row}">
              <img v-if="row.staffPhoto" class="username" :src="row.staffPhoto" alt="">
              <span v-else class="username"> {{ row.username.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column
            prop="mobile"
            label="手机号"
            sortable
          >
            <el-table-column prop="workNumber" label="工号" sortable />
            <el-table-column prop="formOfEmployment" label="聘用形式">
              <template v-slot="{row}">
                <span v-if="row.formOfEmployment === 1"> 正式</span>
                <span v-else> 非正式</span>
              </template>
            </el-table-column>
            <el-table-column prop="departmentName" label="部门" />
            <el-table-column prop="timeOfEntry" label="入职时间" sortable />
            <el-table-column label="操作" width="280px">
              <template v-slot="{row}">
                <el-button size="mini" type="text" @click="$router.push('/employee/detail/' + row.id)">查看</el-button>
                <el-button size="mini" type="text" @click="hexe(row.id)">角色</el-button>
                <el-button size="mini" type="text">删除</el-button>
              </template>
            </el-table-column>
          </el-table-column></el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination layout="total,prev, pager, next" :total="1000" />
        </el-row>
      </div>
    </div>
    <import-excel
      :show-excel-dialog.sync="showExcelDialog"
      @update="syss"
    />
    <dolg
      ref="cx"
      :cruid="cruid"
      :showemp-dialog.sync="showempDialog"
    />
  </div>
</template>

<script>
import FileSaver from 'file-saver'
import { department, expor, sysuser } from '@/api/user'
import { transListToTreeData } from '@/utils/arr'
import importExcel from './components/import-excel.vue'
import Dolg from './components/dolg.vue'
export default {
  name: 'Employee',
  components: { importExcel, Dolg },
  data() {
    return {
      cruid: -1,
      showempDialog: false,
      showExcelDialog: false,
      total: null,
      rows: [],
      obj: {
        page: 1,
        pagesize: 10,
        keyword: null,
        departmentId: null
      },
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.sys()
    this.syss()
  },
  methods: {
    hexe(id) {
      this.showempDialog = true
      // console.log(id)
      this.cruid = id
      this.$nextTick(() => {
        this.$refs.cx.qingqiu()
      })
    },
    daoru() {
      this.showExcelDialog = true
    },
    async expro() {
      const res = await expor()
      console.log(res)
      FileSaver(res)
    },
    handleNodeClick(data, nood) {
      // console.log(data, nood)
      this.obj.departmentId = data.id
      // console.log(this.obj.departmentId)
      this.syss()
      // sysuser(this.obj)
    },
    async syss() {
      const res = await sysuser(this.obj)
      console.log(res)
      this.total = res.data.total
      this.rows = res.data.rows
      console.log(this.rows)
    },
    async sys() {
      const res = await department()
      // console.log('你好', res.data.id)
      // this.data = res.data.rows
      this.data = res.data
      this.data = transListToTreeData(res.data, 0)
      const id = this.data[0].id
      this.$nextTick(() => {
        // this.$refs.tree.setCheckedKeys([id])
        this.$refs.tree.setCurrentKey(id)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
